package com.yxq.mdt.app.modules.personal.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.modules.adapter.UploadImageListAdapter;
import com.yxq.mdt.app.modules.bean.UploadImageBean;
import com.yxq.mdt.app.utils.GlideEngine;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author L
 * @time
 * @describe 意见反馈
 */
public class SuggestActivity extends BaseActivity {

    @BindView(R.id.image_recycler_view)
    RecyclerView imageRecyclerView;
    UploadImageListAdapter uploadImageListAdapter;
    List<UploadImageBean> uploadImageBeanList;
    @BindView(R.id.suggest_tv_type)
    TextView suggestTvType;
    @BindView(R.id.suggest_et_content)
    EditText suggestEtContent;
    @BindView(R.id.suggest_btn_submit)
    Button suggestBtnSubmit;

    @Override
    public int getLayoutId() {
        return R.layout.activity_suggest;
    }

    @Override
    public void initView() {
        //初始化图片上传控件
        imageRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        uploadImageBeanList = new ArrayList<>();
        uploadImageBeanList.add(new UploadImageBean());
        uploadImageListAdapter = new UploadImageListAdapter(uploadImageBeanList);
        imageRecyclerView.setAdapter(uploadImageListAdapter);
        uploadImageListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (uploadImageListAdapter.getItem(position).getItemType() == -1) {
                    selectImages();
                }
            }
        });
        uploadImageListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                List<UploadImageBean> uploadImageBeanList = uploadImageListAdapter.getData();
                if (uploadImageBeanList.get(0).getItemType() != -1) {
                    uploadImageBeanList.remove(position);
                    //第一张不是添加图片，添加按钮
                    List<UploadImageBean> temp = new ArrayList<>();
                    temp.add(new UploadImageBean());
                    temp.addAll(uploadImageBeanList);
                    uploadImageListAdapter.setNewData(temp);
                } else {
                    uploadImageListAdapter.remove(position);
                }
            }
        });
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<LocalMedia> images;
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    images = PictureSelector.obtainMultipleResult(data);
                    parseLocalMedia(images);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的

                    break;
            }
        }
    }

    /**
     * 填充图片到界面
     *
     * @param images
     */
    private void parseLocalMedia(List<LocalMedia> images) {
        if (uploadImageListAdapter.getData().size() == 0) {
            uploadImageListAdapter.getData().add(new UploadImageBean());
        }
        List<UploadImageBean> data = new ArrayList<>();
        UploadImageBean uploadImageBean;
        for (LocalMedia localMedia : images) {
            uploadImageBean = new UploadImageBean();
            uploadImageBean.setItemType(0);
            uploadImageBean.setPath(localMedia.getCompressPath());
            data.add(uploadImageBean);
        }
        uploadImageListAdapter.getData().addAll(data);
        if (uploadImageListAdapter.getData().size() == 7) {
            uploadImageListAdapter.getData().remove(0);
        }
        uploadImageListAdapter.notifyDataSetChanged();
    }

    /**
     * 打开图片选择器
     */
    private void selectImages() {
        //相册
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .loadImageEngine(GlideEngine.createGlideEngine())
                .maxSelectNum(7 - uploadImageListAdapter.getData().size())//最大选择数目 最大6张减去现在已经添加的张数
                .compress(true)
                .minSelectNum(1)
                .imageSpanCount(4)
                .selectionMode(PictureConfig.MULTIPLE)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    @OnClick({R.id.suggest_tv_type, R.id.suggest_btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.suggest_tv_type://弹出类型选择框
                alertDialog();
                break;
            case R.id.suggest_btn_submit:
                break;
        }
    }
    /**
     * 类型选择
     */
    private void alertDialog() {


    }
}
